| | | 1 | | // Copyright (c) 2020-2023 dotBunny Inc. |
| | | 2 | | // dotBunny licenses this file to you under the BSL-1.0 license. |
| | | 3 | | // See the LICENSE file in the project root for more information. |
| | | 4 | | |
| | | 5 | | using System; |
| | | 6 | | using System.Reflection; |
| | | 7 | | |
| | | 8 | | namespace GDX |
| | | 9 | | { |
| | | 10 | | /// <summary> |
| | | 11 | | /// A collection of reflection related helper utilities. |
| | | 12 | | /// </summary> |
| | | 13 | | /// <remarks>Torn about the existence of this utility class, yet alone the conditions dictating it.</remarks> |
| | | 14 | | public static class Reflection |
| | | 15 | | { |
| | | 16 | | /// <summary> |
| | | 17 | | /// <see cref="BindingFlags"/> for a private field. |
| | | 18 | | /// </summary> |
| | | 19 | | public const BindingFlags PrivateFieldFlags = BindingFlags.Instance | BindingFlags.NonPublic; |
| | | 20 | | /// <summary> |
| | | 21 | | /// <see cref="BindingFlags"/> for a private static. |
| | | 22 | | /// </summary> |
| | | 23 | | public const BindingFlags PrivateStaticFlags = BindingFlags.Static | BindingFlags.NonPublic; |
| | | 24 | | /// <summary> |
| | | 25 | | /// <see cref="BindingFlags"/> for a public static. |
| | | 26 | | /// </summary> |
| | | 27 | | public const BindingFlags PublicStaticFlags = BindingFlags.Static | BindingFlags.Public; |
| | | 28 | | |
| | | 29 | | /// <summary> |
| | | 30 | | /// The assembly qualified name for <see cref="UnityEngine.Object" /> |
| | | 31 | | /// </summary> |
| | 2 | 32 | | public static readonly string UnityObjectName = typeof(UnityEngine.Object).AssemblyQualifiedName; |
| | | 33 | | |
| | | 34 | | /// <summary> |
| | | 35 | | /// The assembly qualified name for <see cref="Serializable.SerializableTypes" /> |
| | | 36 | | /// </summary> |
| | 2 | 37 | | public static readonly string SerializedTypesName = |
| | | 38 | | typeof(Serializable.SerializableTypes).AssemblyQualifiedName; |
| | | 39 | | |
| | | 40 | | /// <summary> |
| | | 41 | | /// Returns the default value for a given type. |
| | | 42 | | /// </summary> |
| | | 43 | | /// <param name="type">A qualified type.</param> |
| | | 44 | | /// <returns>The default value.</returns> |
| | | 45 | | public static object GetDefault(this Type type) |
| | 2 | 46 | | { |
| | 2 | 47 | | if (type.IsClass || !type.IsValueType) |
| | 1 | 48 | | { |
| | 1 | 49 | | return null; |
| | | 50 | | } |
| | 1 | 51 | | return Activator.CreateInstance(type); |
| | 2 | 52 | | } |
| | | 53 | | |
| | | 54 | | /// <summary> |
| | | 55 | | /// Returns a qualified type.. |
| | | 56 | | /// </summary> |
| | | 57 | | /// <param name="type">The full name of a type.</param> |
| | | 58 | | /// <returns>A <see cref="System.Type"/> if found.</returns> |
| | | 59 | | public static Type GetType(string type) |
| | 29 | 60 | | { |
| | 29 | 61 | | Assembly[] loadedAssemblies = AppDomain.CurrentDomain.GetAssemblies(); |
| | 29 | 62 | | int loadedAssembliesCount = loadedAssemblies.Length; |
| | 5126 | 63 | | for (int i = 0; i < loadedAssembliesCount; i++) |
| | 2561 | 64 | | { |
| | 2561 | 65 | | Type targetType = loadedAssemblies[i].GetType(type); |
| | 2561 | 66 | | if (targetType != null) |
| | 27 | 67 | | { |
| | 27 | 68 | | return targetType; |
| | | 69 | | } |
| | 2534 | 70 | | } |
| | 2 | 71 | | return null; |
| | 29 | 72 | | } |
| | | 73 | | |
| | | 74 | | /// <summary> |
| | | 75 | | /// Invokes a known static method. |
| | | 76 | | /// </summary> |
| | | 77 | | /// <param name="type">The explicit type of the static class.</param> |
| | | 78 | | /// <param name="method">The name of the method to invoke.</param> |
| | | 79 | | /// <param name="parameters">Any parameters that should be passed to the method?</param> |
| | | 80 | | /// <param name="flags">The <paramref name="method"/>'s access flags.</param> |
| | | 81 | | /// <returns>An <see cref="object"/> of the return value. This can be null.</returns> |
| | | 82 | | public static object InvokeStaticMethod(string type, string method, object[] parameters = null, |
| | | 83 | | BindingFlags flags = PublicStaticFlags) |
| | 6 | 84 | | { |
| | 6 | 85 | | Type targetType = GetType(type); |
| | 6 | 86 | | if (targetType != null) |
| | 5 | 87 | | { |
| | 5 | 88 | | MethodInfo targetMethod = targetType.GetMethod(method, flags); |
| | 5 | 89 | | if (targetMethod != null) |
| | 4 | 90 | | { |
| | 4 | 91 | | return targetMethod.Invoke(null, parameters ?? Core.EmptyObjectArray); |
| | | 92 | | } |
| | 1 | 93 | | } |
| | 2 | 94 | | return null; |
| | 6 | 95 | | } |
| | | 96 | | |
| | | 97 | | /// <summary> |
| | | 98 | | /// Invoke a known private method on an object. |
| | | 99 | | /// </summary> |
| | | 100 | | /// <param name="targetObject">The ambiguous object to invoke a method on.</param> |
| | | 101 | | /// <param name="method">The name of the method to invoke.</param> |
| | | 102 | | /// <param name="parameters">Any parameters that should be passed to the method?</param> |
| | | 103 | | /// <param name="flags">The <paramref name="method"/>'s access flags.</param> |
| | | 104 | | /// <returns>An <see cref="object"/> of the return value. This can be null.</returns> |
| | | 105 | | public static object InvokeMethod(object targetObject, string method, object[] parameters = null, |
| | | 106 | | BindingFlags flags = PrivateFieldFlags) |
| | 11 | 107 | | { |
| | 11 | 108 | | Type targetType = targetObject.GetType(); |
| | 11 | 109 | | MethodInfo targetMethod = targetType.GetMethod(method, flags); |
| | 11 | 110 | | return targetMethod != null ? targetMethod.Invoke(targetObject, parameters ?? Core.EmptyObjectArray) : null; |
| | 11 | 111 | | } |
| | | 112 | | |
| | | 113 | | /// <summary> |
| | | 114 | | /// Set the field or property value of a specific <paramref name="targetObject"/>, which may not be |
| | | 115 | | /// normally accessible. |
| | | 116 | | /// </summary> |
| | | 117 | | /// <param name="targetObject">The instanced object which will have it's field or property value set.</param> |
| | | 118 | | /// <param name="name">The field or property's name to set.</param> |
| | | 119 | | /// <param name="value">The value to set the field or property to.</param> |
| | | 120 | | /// <param name="fieldFlags">The field's access flags.</param> |
| | | 121 | | /// <param name="propertyFlags">The property's access flags.</param> |
| | | 122 | | /// <returns>true/false if the value was set.</returns> |
| | | 123 | | public static bool SetFieldOrPropertyValue(object targetObject, string name, object value, |
| | | 124 | | BindingFlags fieldFlags = PrivateFieldFlags, BindingFlags propertyFlags = PrivateFieldFlags) |
| | 6 | 125 | | { |
| | 6 | 126 | | if (targetObject == null) |
| | 1 | 127 | | return false; |
| | | 128 | | |
| | 5 | 129 | | Type type = targetObject.GetType(); |
| | 5 | 130 | | FieldInfo f = type.GetField(name, fieldFlags); |
| | 5 | 131 | | if (f != null) |
| | 2 | 132 | | { |
| | 2 | 133 | | f.SetValue(targetObject, value); |
| | 2 | 134 | | return true; |
| | | 135 | | } |
| | | 136 | | |
| | 3 | 137 | | PropertyInfo p = type.GetProperty(name,propertyFlags); |
| | 3 | 138 | | if (p != null) |
| | 2 | 139 | | { |
| | 2 | 140 | | p.SetValue(targetObject, value); |
| | 2 | 141 | | return true; |
| | | 142 | | } |
| | | 143 | | |
| | 1 | 144 | | return false; |
| | 6 | 145 | | } |
| | | 146 | | |
| | | 147 | | /// <summary> |
| | | 148 | | /// Set the field value of a specific <paramref name="targetObject"/>, which may not be normally accessible. |
| | | 149 | | /// </summary> |
| | | 150 | | /// <param name="targetObject">The instanced object which will have it's field value set; use a null value if th |
| | | 151 | | /// <param name="type">The explicit type of the <paramref name="targetObject"/>.</param> |
| | | 152 | | /// <param name="name">The field's name to set.</param> |
| | | 153 | | /// <param name="value">The value to set the field to.</param> |
| | | 154 | | /// <param name="flags">The field's access flags.</param> |
| | | 155 | | /// <returns>true/false if the value was set.</returns> |
| | | 156 | | public static bool SetFieldValue(object targetObject, Type type, string name, object value, |
| | | 157 | | BindingFlags flags = PrivateFieldFlags) |
| | 6 | 158 | | { |
| | 6 | 159 | | if (type != null) |
| | 5 | 160 | | { |
| | 5 | 161 | | FieldInfo field = type.GetField(name, flags); |
| | 5 | 162 | | if (field != null) |
| | 4 | 163 | | { |
| | 4 | 164 | | field.SetValue(targetObject, value); |
| | 4 | 165 | | return true; |
| | | 166 | | } |
| | 1 | 167 | | } |
| | 2 | 168 | | return false; |
| | 6 | 169 | | } |
| | | 170 | | |
| | | 171 | | /// <summary> |
| | | 172 | | /// Set the property value of a specific <paramref name="targetObject"/>, which may not be normally accessib |
| | | 173 | | /// </summary> |
| | | 174 | | /// <param name="targetObject">The instanced object which will have it's property value set; use a null value if |
| | | 175 | | /// <param name="type">The type of the <paramref name="targetObject"/>.</param> |
| | | 176 | | /// <param name="name">The property's name to set.</param> |
| | | 177 | | /// <param name="value">The value to set the property to.</param> |
| | | 178 | | /// <param name="flags">The property's access flags.</param> |
| | | 179 | | /// <returns>true/false if the value was set.</returns> |
| | | 180 | | public static bool SetPropertyValue(object targetObject, Type type, string name, object value, |
| | | 181 | | BindingFlags flags = PrivateFieldFlags) |
| | 4 | 182 | | { |
| | 4 | 183 | | if (type != null) |
| | 3 | 184 | | { |
| | 3 | 185 | | PropertyInfo property = type.GetProperty(name, flags); |
| | 3 | 186 | | if (property != null) |
| | 2 | 187 | | { |
| | 2 | 188 | | property.SetValue(targetObject, value); |
| | 2 | 189 | | return true; |
| | | 190 | | } |
| | 1 | 191 | | } |
| | 2 | 192 | | return false; |
| | 4 | 193 | | } |
| | | 194 | | |
| | | 195 | | /// <summary> |
| | | 196 | | /// Try to access the field value of a specific <paramref name="targetObject"/>, which may not be normally a |
| | | 197 | | /// </summary> |
| | | 198 | | /// <remarks></remarks> |
| | | 199 | | /// <param name="targetObject">The instanced object which will have it's field value read; use a null value if t |
| | | 200 | | /// <param name="type">The qualified type of the <paramref name="targetObject"/>.</param> |
| | | 201 | | /// <param name="name">The field's name to read.</param> |
| | | 202 | | /// <param name="returnValue">The returned value from the field, the default value if the field was unable to be |
| | | 203 | | /// <param name="flags">The field's access flags.</param> |
| | | 204 | | /// <typeparam name="T">The type of data being read from the field.</typeparam> |
| | | 205 | | /// <returns>true/false if the process was successful.</returns> |
| | | 206 | | public static bool TryGetFieldValue<T>(object targetObject, Type type, string name, out T returnValue, BindingFl |
| | 7 | 207 | | { |
| | 7 | 208 | | if (type == null) |
| | 1 | 209 | | { |
| | 1 | 210 | | returnValue = default; |
| | 1 | 211 | | return false; |
| | | 212 | | } |
| | 6 | 213 | | FieldInfo fieldInfo = type.GetField(name, flags); |
| | 6 | 214 | | if (fieldInfo == null) |
| | 1 | 215 | | { |
| | 1 | 216 | | returnValue = default; |
| | 1 | 217 | | return false; |
| | | 218 | | } |
| | 5 | 219 | | returnValue = (T)fieldInfo.GetValue(targetObject); |
| | 5 | 220 | | return true; |
| | 7 | 221 | | } |
| | | 222 | | |
| | | 223 | | /// <summary> |
| | | 224 | | /// Try to access the field or property value of a specific <paramref name="targetObject"/>, which may not |
| | | 225 | | /// be normally accessible. |
| | | 226 | | /// </summary> |
| | | 227 | | /// <remarks>Useful for when you really do not know the <see cref="System.Type"/>.</remarks> |
| | | 228 | | /// <param name="targetObject">The instanced object which will have it's field or property value read.</param> |
| | | 229 | | /// <param name="name">The field or property's name to read.</param> |
| | | 230 | | /// <param name="returnValue">The returned value from the field or property, the default value if the property w |
| | | 231 | | /// <param name="fieldFlags">The field's access flags.</param> |
| | | 232 | | /// <param name="propertyFlags">The property's access flags.</param> |
| | | 233 | | /// <returns>true/false if a value was found.</returns> |
| | | 234 | | public static bool TryGetFieldOrPropertyValue(object targetObject, string name, out object returnValue, |
| | | 235 | | BindingFlags fieldFlags = PrivateFieldFlags, BindingFlags propertyFlags = PrivateFieldFlags) |
| | 7 | 236 | | { |
| | 7 | 237 | | if (targetObject == null) |
| | 1 | 238 | | { |
| | 1 | 239 | | returnValue = null; |
| | 1 | 240 | | return false; |
| | | 241 | | } |
| | | 242 | | |
| | 6 | 243 | | Type type = targetObject.GetType(); |
| | 6 | 244 | | FieldInfo f = type.GetField(name, fieldFlags); |
| | 6 | 245 | | if (f != null) |
| | 2 | 246 | | { |
| | 2 | 247 | | returnValue = f.GetValue(targetObject); |
| | 2 | 248 | | return true; |
| | | 249 | | } |
| | | 250 | | |
| | 4 | 251 | | PropertyInfo p = type.GetProperty(name,propertyFlags); |
| | 4 | 252 | | if (p != null) |
| | 3 | 253 | | { |
| | 3 | 254 | | returnValue = p.GetValue(targetObject); |
| | 3 | 255 | | return true; |
| | | 256 | | } |
| | | 257 | | |
| | 1 | 258 | | returnValue = default; |
| | 1 | 259 | | return false; |
| | 7 | 260 | | } |
| | | 261 | | |
| | | 262 | | /// <summary> |
| | | 263 | | /// Try to get a property value from <paramref name="targetObject"/>, which may not be normally accessible. |
| | | 264 | | /// </summary> |
| | | 265 | | /// <param name="targetObject">The instanced object which will have it's property value read; use a null value i |
| | | 266 | | /// <param name="type">The explicit type of the <paramref name="targetObject"/>.</param> |
| | | 267 | | /// <param name="name">The property's name to read.</param> |
| | | 268 | | /// <param name="returnValue">The returned value from the property, the default value if the property was unable |
| | | 269 | | /// <param name="flags">The property's access flags.</param> |
| | | 270 | | /// <typeparam name="T">The type of data being read from the property.</typeparam> |
| | | 271 | | /// <returns>true/false if the process was successful.</returns> |
| | | 272 | | public static bool TryGetPropertyValue<T>(object targetObject, Type type, string name, out T returnValue, Bindin |
| | 5 | 273 | | { |
| | 5 | 274 | | if (type == null) |
| | 1 | 275 | | { |
| | 1 | 276 | | returnValue = default; |
| | 1 | 277 | | return false; |
| | | 278 | | } |
| | 4 | 279 | | PropertyInfo propertyInfo = type.GetProperty(name, flags); |
| | 4 | 280 | | if (propertyInfo == null) |
| | 1 | 281 | | { |
| | 1 | 282 | | returnValue = default; |
| | 1 | 283 | | return false; |
| | | 284 | | } |
| | | 285 | | |
| | 3 | 286 | | returnValue = (T)propertyInfo.GetValue(targetObject); |
| | 3 | 287 | | return true; |
| | 5 | 288 | | } |
| | | 289 | | } |
| | | 290 | | } |